<?php
namespace app\admin\validate;

/**
 * 用户验证
 */
class User extends Main
{
    protected $rule = [
        'header_img' =>'require',
        'mobile' => 'require|mobile|checkMobile',
        'nickname' => 'require',
        'password' => 'require',
        'status' => 'require|in:0,1',
    ];

    protected $message = [
        'header_img.require' => '请上传头像',
        'mobile.require' => '请填写手机号',
        'mobile.mobile' => '手机号格式错误',
        'nickname.require' => '请填写昵称',
        'password.require' => '请填写密码',
        'status' => '请选择用户状态',
    ];

    protected $scene = [
        'add' =>  ['header_img', 'mobile', 'nickname', 'password', 'status'],
        'edit' =>  ['header_img', 'mobile', 'nickname', 'status'],
        'del' => ['id']
    ];

    protected function checkMobile($value, $rule, $data)
    {
        if($data['id']??false){
            if(count(\think\Db::name('user')->where('mobile', $value)->where('id', '<>', $data['id'])->column('id'))){
                return '手机号已存在';
            }
        }elseif(count(\think\Db::name('user')->where('mobile', $value)->column('id'))){
            return '手机号已存在';
        }
        return true;
    }
}